<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="50%" top="5vh" @close.native="cancel"
      :close-on-click-modal="false">
      <el-form ref="ruleForm" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="共享资源名称：" prop="title">
          <el-input v-model="form.title" placeholder="请输入" :disabled="isLook == 'look'" />
        </el-form-item>
        <el-form-item label="咨询用户：" prop="name">
          <el-input v-model="form.name" placeholder="请输入" :disabled="isLook == 'look'" />
        </el-form-item>
        <el-form-item label="咨询时间：" prop="createdTime">
          <el-date-picker style="width:100%" v-model="form.createdTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择日期" :disabled="isLook == 'look'">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="咨询内容：" prop="content">
          <el-input v-model="form.content" type="textarea" rows="7" placeholder="请输入内容" :disabled="isLook == 'look'" />
        </el-form-item>
        <el-form-item label="答复内容：" prop="replyContent" v-if="isLook !== 'add'">
          <el-input v-model="form.replyContent" type="textarea" rows="10" placeholder="请输入答复内容"
            :disabled="isLook == 'look'" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="isLook !== 'look'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addConsult,
  updateConsult
} from '@/api/yqgx/consult'
export default {
  data() {
    return {
      dialogVisible: false,
      isLook: '',
      accept: '*',
      title: '',
      form: {
        title: '',
        name: '',
        createdTime: '',
        content: '',
        replyContent: '',
      },
      facilityType: ['政策法规', '管理制度'],
      file: null,
      rules: {
        title: [
          { required: true, message: '不能为空！', trigger: 'blur' }
        ],
        name: [{ required: true, message: '不能为空！', trigger: 'blur' }],
        createdTime: [{ required: true, message: '不能为空！', trigger: 'blur' }],
        content: [
          { required: true, message: '不能为空！', trigger: 'blur' }
        ],
      }
    }
  },
  created() { },
  methods: {
    show(obj) {
      this.dialogVisible = true
      this.title = obj.title
      this.isLook = obj.isLook
      if (obj.isLook == 'add') {
        this.form.title = obj.data.title
      } else {
        this.form = Object.assign(obj.data)
      }
    },
    submitForm() {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          if (this.form.id) {
            updateConsult(this.form).then(() => {
              this.cancel()
            })
          } else {
            addConsult(this.form).then(() => {
              this.cancel()
            })
          }
        } else {
          return false
        }
      })
    },
    cancel() {
      this.form = this.$options.data().form
      this.$refs.ruleForm.clearValidate()
      this.dialogVisible = false
      this.isLook = false
      this.file = null
      this.$emit('close')
    },
    handleChange(file) {
      this.file = file.raw
      this.form.fileName = file.name
    },
    // 多次上传覆盖
    handleExceed(file) {
      console.log('[ file ] >', file)
      this.$refs.uploadRef.clearFiles()
      // this.handleChange(file[0])
      this.file = file[0]
      this.form.fileName = file[0].name
    },
    downloadLink(item) {
      this.downloadByUrl(item.fileName, item.filePath)
    },
    closeFile() {
      this.file = null
      this.$refs.uploadRef.clearFiles()
    }
  }
}
</script>

<style lang="less" scoped>
:deep(.el-table .cell) {
  font-size: 16px;
}

:deep(.el-table th > .cell) {
  font-size: 18px;
}
</style>
